<?php

namespace Api\Controller;
use Api\Common\BaseController;

class ProjectController extends BaseController{
	
	
	/**
	* 添加项目
	* @return
	*/
	
	function addproject(){
		$data=I('post.');
		$num=M('project')->count();
		$var=sprintf("%09d",$num+1);
		$code="#".$var;
		if($data!=null){
			$data=array(
				'code'=>$code,
				'user_id'=>$data['user_id'],
				'p_name'=>$data['p_name'],
				'p_type'=>$data['p_type'],
				'p_price'=>$data['p_price'],
				'p_ad'=>$data['p_ad'],
				'p_offtime'=>$data['p_offtime'],
				'p_ds'=>html_entity_decode($data['p_ds']),
				'p_fly'=>$data['p_fly'],
				'p_cycle'=>$data['p_cycle'],
				'p_username'=>$data['p_username'],
				'p_phone'=>$data['p_phone'],
				'status'=>0,
				'ctime'=>time(),
			);
			
			$res=M('project')->add($data);
			if($res){
				$this->ajaxReturn(array('s'=>true,'m'=>"添加成功"));
			}else{
				$this->ajaxReturn(array('s'=>false,'m'=>"添加失败"));
			}	
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"提交异常，未获取到数据，请重新提交"));
		}
	}
	
	/**
	* 获取项目列表
	* @return
	*/
	
	function projectlist(){

		$data=I('post.');
		$where['status']=1;
		if($data!=null){
			if($data['page']!=""){
				$num=$data['page'];
				if($num==1){
					$bin=0;
					$off=10;
				}else{
					$bin=($num*10)-10;
					$off=$num*10;
				}
			}
			if($data['p_type']!=""){
				$where['p_type']=$data['p_type'];	
			}
			
			$project=M("project")->where($where)->order('id desc')->limit($bin,$off)->select();
			if(!$project){
				$this->ajaxReturn(array('s'=>false,'m'=>'服务器忙'));
			}
		}else{
			$project=M("project")->order('id desc')->limit(10)->select();
		}
		
		foreach($project as $li){
			$user=M('user')->where(array("id"=>$li['user_id']))->find();
			if($li['p_ad']==0){
				$lit['p_ad']="尚未开发";
			}
			$lit['id']=$li['id'];
			$lit['user_name']=$user['username'];
			$lit['user_headpic']=$user['headpic'];
			$lit['ctime']=date('Y-m-d',$li['ctime']);
			$lit['p_name']=$li['p_name'];
			$lit['p_ds']=$li['p_ds'];
			$lit['p_ad']=$li['p_ad'];
			$lit['p_offtime']=$li['p_offtime'];
			
			$arr[]=$lit;
		}
		$this->ajaxReturn(array('s'=>true,'d'=>$arr));
	}
	
	/**
	* 获取项目详情
	* @return
	*/
	
	function projectshow(){

		$pid=I('id');
		$where['status']=1;
		$where['id']=$pid;
		$project=M("project")->where($where)->find();
		if(!$project){
				$this->ajaxReturn(array('s'=>false,'m'=>'获取错误'));
			}
			$projectac=M("project_user")->where(array("p_id"=>$pid))->find();
			$user=M('user')->where(array("id"=>$project['user_id']))->find();
			
			$pm=M('user')->where(array("id"=>$projectac['pm_id']))->find();
			
			$pg_id= explode(',',$projectac['pg_id']); 
			$pg="";
			foreach ($pg_id as $k=>$id) {
				$pgname=M('user')->where(array("id"=>$id))->find();
				if($k>0){
					$pg.="/".$pgname['username'];
				}else{
					$pg.=$pgname['username'];
				}
			}
			$project['ctime']=date('Y-m-d',$project['ctime']);
			$project['offtime']=date('Y-m-d',$project['offtime']);
			switch ($project['p_type']) {
				case '1':
					$project['p_type']="App";
					break;
				case '2':
					$project['p_type']="微信公众号";
					break;
				case '3':
					$project['p_type']="web";
					break;
				case '4':
					$project['p_type']="小程序";
					break;
				case '5':
					$project['p_type']="H5";
					break;
				default:
					$project['p_type']="其他";
					break;
			}
			switch ($project['p_state']) {
				case '0':
					$project['p_state']="待跟进";
					break;
				case '1':
					$project['p_state']="已解决";
					break;
				case '2':
					$project['p_state']="结束";
					break;
				case '2':
					$project['p_state']="关闭";
					break;
				default:
					$project['p_state']="已过期";
					break;
			}
			if($project['p_ad']==0){
				$project['p_ad']="尚未开发"	;
			}
			$project['user_name']=$user['username'];
			$project['user_headpic']=$user['headpic'];
			$project['pm']=$pm['username'];
			$project['pg']=$pg;
			$this->ajaxReturn(array('s'=>true,'d'=>$project));
	}
	
	
	
	
	
	
	
	/**
	* 上传文件
	* @return
	*/
	function  uploadfile(){
		set_time_limit(0);
		
		$file = $_FILES['Filedata'];
		
		if($file['error']!=0){
			$this->ajaxReturn(array('s'=>false,'m'=>"上传文件不能为空"));
		}
		$finfo = pathinfo($file['name']);
		$exe = strtolower($finfo['extension']);//文件名后缀
		$SAVE_PATH = 'Public/upload/file/';
		$filename = $SAVE_PATH.date('ymd_his_').mt_rand(1000,9999).'.'.$exe;
		
		$fileTypes = array('zip','rar');
		if(!in_array($exe,$fileTypes)){
			$this->ajaxReturn(array('s'=>false,'m'=>"只能上传后缀为zip或rar的压缩文件"));
			
		}
		$fCont = file_get_contents($file['tmp_name']);  
		
		$size=substr($file['size']/1024000,0,3); 
		
		if($size>500){
			$this->ajaxReturn(array('s'=>false,'m'=>"文件不能大于500MB"));
			
		}
		$rs = move_uploaded_file($file['tmp_name'],$filename);
		if(!$rs){
			$this->ajaxReturn(array('s'=>false,'m'=>"文件上传失败"));
		}else{
			$this->ajaxReturn(array('s'=>true,'m'=>"文件上传成功","d"=>$filename));
		}	
	}
	
	/**
	* 项目经理指定程序
	* @return
	*/
	
	function savepm(){
		$data=I('post.');
		$where=array('p_id'=>$data['pid']);
		$res=M('project_user')->where($where)->find();
		if($res){
//			$save=array(
//			'pm_id'=>$data['uid']
//			);
//			try{
//				$rest=M("project_user")->where($where)->save($save);
//				$this->ajaxReturn(array('s'=>true));
//			}catch( Exception $e){
//				$this->ajaxReturn(array('s'=>false));
//			}
		}else{
//			$add=array(
//			'p_id'=>$data['pid'],
//			'pm_id'=>$data['uid']
//			);
//			$rest=M("project_user")->add($add);
//			if($rest){
//				$this->ajaxReturn(array('s'=>true));
//			}
		};
	}
	
	
}